package com.crmweb.controller;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.crmweb.entity.Askers;
import com.crmweb.entity.Netfollows;
import com.crmweb.entity.PageVo;
import com.crmweb.entity.Result;
import com.crmweb.entity.Roles;
import com.crmweb.entity.Userchecks;
import com.crmweb.entity.Users;
import com.crmweb.service.AskersService;
import com.crmweb.service.UserCheckService;
import com.crmweb.service.UsersService;
import com.crmweb.tool.ConstantUtil;

@RestController
public class UserCkeckController {

	@Resource
	private UserCheckService ucs;
	
	@Resource
	private UsersService us;
	
	@Resource
	private AskersService as;
	
	@RequestMapping("sysM/selAllUserchecksByWhere")
	public Map<String, Object> selAllUserchecksByWhere(Userchecks  uck, HttpServletRequest request){
		
		String page = request.getParameter("page");
		String pageSize = request.getParameter("limit");
		PageVo vo = new PageVo();
		vo.setPage(Integer.parseInt(page));
		vo.setPageSize(Integer.parseInt(pageSize));
		
		uck.setVo(vo);
		List<Userchecks> list = ucs.selAllUserchecksByWhere(uck);
		Map<String, Object> map = new HashMap<>();
		map.put("code", 0);
		map.put("data", list);
		map.put("count", ucs.getcountByWhere(uck));
		
		return map;
		
	}
	//员工签退
	@RequestMapping("sysM/userCheckOut")
	public Object userCheckOut(int id) {
		int i = ucs.userCheckOut(id);
		if(i>0) {
			Result res = new Result("签退成功","",true);
			return res;
		}else {
			Result res = new Result("签退失败","",false);
			return res;
		}
		
	}
	
	/**
	 * 新增员工签到信息
	 */
	@RequestMapping("createUserCheck")
	public Object createUserCheck(HttpServletRequest request) {
		
		Result res = new Result();
			HttpSession session=request.getSession(); 
			Users users=(Users)session.getAttribute(ConstantUtil.USERS);
			String userId = users.getId();
			List<String> ridlist = us.selRoleIdByUid(userId);
			List<Roles> rlist = new ArrayList<Roles>();
			for (String string : ridlist) {
				Roles rname = us.selRoleByRoleId(string);
				rlist.add(rname);
			}
			List<String> rnamelist = new ArrayList<>();
			for (Roles roles : rlist) {
				String rname = roles.getName();
				rnamelist.add(rname);
			}
			if(rnamelist!=null &&  rnamelist.contains("咨询师")){
				//插入签到信息的同时更新asker表相关咨询师角色的人员的信息（每次签到都更新对应的咨询师为未分配学生）
				  as.updaAskerCheckInTime(userId,"已签到","否");
			}
			
			
			// 创建之前先查询该用户是否已签到，如果已签到就更新
			Userchecks userCheck = ucs.getUserCheckByUserId(userId);
			if (userCheck != null) {
				userCheck.setIsCancel("否");
				userCheck.setCheckState("已签到");
				//userCheck.setCheckInTime("dd");
				int i = ucs.updateUserCheck(userCheck);
				if(i>0) {
					res = new Result("签到成功","",true);
					return res;
				}else {
					 res = new Result("签到失败","",false);
					return res;
				}
			} else {
				String userName = users.getLoginName();
				Userchecks uCheck = new Userchecks();
				uCheck.setUserId(userId);
				uCheck.setUserName(userName);
				uCheck.setCheckState("已签到");
				int flag = ucs.saveUserCheck(uCheck);
				if(flag>0) {
					 res = new Result("签到成功","",true);
					return res;
				}else {
					 res = new Result("签到失败","",false);
					return res;
				}
			}
		
	}
	
	//初始化查看员工签到信息
	@RequestMapping("initIsCheckIn")
	public Object initIsCheckIn(HttpServletRequest request) {
			
			Map<String, Object> map = new HashMap<>();
		
			HttpSession session=request.getSession(); 
			Users users=(Users)session.getAttribute(ConstantUtil.USERS);
			String userId = users.getId();
			Userchecks userCheck = ucs.getUserCheckByUserId(userId);
			
			if(userCheck !=null) {
				map.put("success", true);
				map.put("message", userCheck.getIsCancel());
			}
			
			return map;
	}
	@RequestMapping("UserCheckout")
	public Object UserCheckout(HttpServletRequest request) {
		
				Result res = new Result();
		
				HttpSession session=request.getSession(); 
				Users users=(Users)session.getAttribute(ConstantUtil.USERS);
				String userId = users.getId();
				List<String> ridlist = us.selRoleIdByUid(userId);
				List<Roles> rlist = new ArrayList<Roles>();
				for (String string : ridlist) {
					Roles rname = us.selRoleByRoleId(string);
					rlist.add(rname);
				}
				List<String> rnamelist = new ArrayList<>();
				for (Roles roles : rlist) {
					String rname = roles.getName();
					rnamelist.add(rname);
				}
				
				
				if(rnamelist!=null &&  rnamelist.contains("咨询师")){
					//插入签到信息的同时更新asker表相关咨询师角色的人员的信息（每次签到都更新对应的咨询师为未分配学生）
					as.updaAskerCheckInTime(userId,"已签退","是");
				}
				// 创建之前先查询该用户是否已签到，如果已签到就更新
				Userchecks userCheck = ucs.getUserCheckByUserId(userId);
				String checkInTime = userCheck.getCheckInTime();
				
				Userchecks uck = new Userchecks();
				uck.setUserId(userId);
				uck.setCheckInTime(checkInTime);
				uck.setCheckState("已签退");
				uck.setIsCancel("是");
				int i = ucs.updateUserCheckByCondition(uck);
				if(i>0) {
					res = new Result("签退成功","",true);
					return res;
				}else {
					 res = new Result("签退失败","",false);
					return res;
				}
		
	}
	
	
}
